<?php
class user extends MY_Controller{
    function user(){
        parent::MY_Controller();
        $this->data['title'] = "Administrasi User";
        $this->load->model('m_user');
        $this->load->model('m_menu');
    }
    
    var $data;
    var $limit = 10;
    function index(){
        $this->get_user();
    }
    function get_user($offset = 0){
        $this->data['content'] = "user/v_user";
        
        $uri_segment = 3;
        if($offset != 0){
          $offset = $this->uri->segment($uri_segment);  
        }
        $num_rows = $this->m_user->getCountRows();
        $this->data['listUser'] = $this->m_user->getListUser($offset,$this->limit);
        $config['base_url'] = site_url("user/get_user");
        $config['uri_segment'] = $uri_segment;
        $config['total_rows'] = $num_rows;
        $config['per_page'] = $this->limit;
        $this->pagination->initialize($config);
        $this->data['pagination'] = $this->pagination->create_links();
        $this->data['offset'] = $offset;
        
        $this->render($this->data);
    }
    function add_user_form($offset = 0){
        $this->data['content'] = "user/v_user";
        $this->data['subLink'] = "user/addUserProses";
        $this->data['formTitle'] = "Form Tambah Data User";
        $this->data['formView'] = "user/addUserForm";
        
        $uri_segment = 3;
        if($offset != 0){
            $offset = $this->uri->segment($uri_segment);
        }
        $num_rows = $this->m_user->getCountRows();
        $this->data['listUser'] = $this->m_user->getListUser($offset,$this->limit);
        $config['base_url'] = site_url("user/add_user_form");
        $config['uri_segment'] = $uri_segment;
        $config['total_rows'] = $num_rows;
        $config['per_page'] = $this->limit;
        $this->pagination->initialize($config);
        $this->data['pagination'] = $this->pagination->create_links();
        $this->data['offset'] = $offset;
        
        $this->render($this->data);
    }
    function edit_user_form($id,$offset = 0){
        $this->data['content'] = "user/v_user";
        $this->data['subLink'] = "user/editUserProses";
        $this->data['formTitle'] = "Form Edit Data User";
        $this->data['formView'] = "user/editUserForm";
        $this->data['dataInput'] = $this->m_user->getUserById($id);
        
        $uri_segment = 4;
        if($offset != 0){
          $offset = $this->uri->segment($uri_segment);  
        }
        $num_rows = $this->m_user->getCountRows();
        $this->data['listUser'] = $this->m_user->getListUser($offset,$this->limit);
        $config['base_url'] = site_url("user/edit_user_form/".$id);
        $config['uri_segment'] = $uri_segment;
        $config['total_rows'] = $num_rows;
        $config['per_page'] = $this->limit;
        $this->pagination->initialize($config);
        $this->data['pagination'] = $this->pagination->create_links();
        $this->data['offset'] = $offset;
        
        $this->render($this->data);
    }
    function edit_privilege($id){
        $this->data['content'] = "user/editPrivilegeForm";
        $this->data['titlePage'] = "Pengaturan User Privilege";
        $this->data['subLink'] = "user/editPrivilegeProses";
        $this->data['formTitle'] = "User Privilege";
        $this->data['linkBack'] = site_url('user/get_user');
        $this->data['dataInput'] = $this->m_user->getUserById($id);
        $this->data['listModule'] = $this->m_menu->getModule();
        
        $this->render($this->data);
    }
    function editPrivilegeProses(){
        $update = $this->m_user->editPrivilege($_POST);
        
        if($update == TRUE){
            $this->session->set_flashdata("message",1);
        }else{
            $this->session->set_flashdata("message",2);
        }
        
        redirect("user/edit_privilege/".$_POST['idUser']);
    }
    function addUserProses(){
        $data = array('id_member' => $this->input->post('idMember'),
            'username' => $this->input->post('username'),
            'password' => md5('1234'),
            'input_date' => date('Y-m-d'),
            'status' => '1');
        $insert = $this->m_user->addUser($data);
        if($insert == TRUE){
            $this->session->set_flashdata("message",1);
        }else{
            $this->session->set_flashdata("message",2);
        }
        
        redirect("user");
    }
    function editUserProses(){
        $data = array('id_member' => $this->input->post('idMember'),
            'username' => $this->input->post('username'));
        $update = $this->m_user->editUser($data);
        if($update == TRUE){
           $this->session->set_flashdata("message",1); 
        }else{
           $this->session->set_flashdata("message",2);
        }
        
        redirect("user");
    }
    function aktivasi($idUser,$value){
        $data = array("id"=>$idUser,
            "status"=>"$value");
        
        $aktivasi = $this->m_user->aktivasi($data);
        if($aktivasi == TRUE){
           $this->session->set_flashdata("message",1);  
        }else{
           $this->session->set_flashdata("message",2);
        }
        
        redirect("user");
    }
    function resetPassword($id){
        $reset = $this->m_user->resetPassword($id);
        if($reset == TRUE){
            $this->session->set_flashdata("message",1); 
        }else{
            $this->session->set_flashdata("message",2);
        }
        redirect('user');
    }
}
?>
